package cc.md888.app.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;

//@Profile(value = {"!prod & !tl"})
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(
                        Lists.newArrayList(
                                new ParameterBuilder().parameterType("header")
                                        .required(false)
                                        .description("设置编号")
                                        .modelRef(new ModelRef("string"))
                                        .defaultValue("")
                                        .name("X-Device-Id")
                                        .build()
                        )
                )
                .securitySchemes(Collections.singletonList(securityScheme()))
                .securityContexts(Collections.singletonList(securityContext()))
//                .host("xylearn-api.javaobj.com")
                .apiInfo(apiInfo())
                .groupName("管理后台")
                .select()
                .apis(
                        input -> RequestHandlerSelectors.basePackage("cc.md888.app.web.controller").or(
                                RequestHandlerSelectors.basePackage("top.maodou168.cloudphone.web")).test(input)
                )
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build();

    }


    @Bean
    SecurityScheme securityScheme() {
        return new ApiKey("ApiKey", "X-Api-Key", "header");
    }

    @Bean
    SecurityContext securityContext() {
        SecurityReference securityReference = SecurityReference.builder()
                .reference("ApiKey")
                .scopes(new AuthorizationScope[]{})
                .build();

        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(securityReference))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("毛豆工具箱")
                .description("毛豆工具箱系统接口文档")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}
